package com.ashlikun.okhttputils.simple

import android.app.Application
import com.ashlikun.okhttputils.http.ExecuteCall
import com.ashlikun.okhttputils.http.HttpException
import com.ashlikun.okhttputils.http.OkHttpManage
import com.ashlikun.okhttputils.http.callback.AbsCallback
import com.ashlikun.okhttputils.http.callback.OkHttpCallback
import com.ashlikun.okhttputils.http.cookie.CookieJarImpl
import com.ashlikun.okhttputils.http.cookie.store.DBCookieStore
import com.ashlikun.okhttputils.http.download.DownloadManager
import com.ashlikun.okhttputils.http.request.HttpRequest
import com.ashlikun.okhttputils.retrofit.Retrofit
import com.ashlikun.okhttputils.simple.interceptor.LoggingInterceptor
import com.ashlikun.okhttputils.simple.interceptor.MarvelSigningInterceptor
import com.ashlikun.orm.LiteOrmUtil
import kotlinx.coroutines.*
import okhttp3.OkHttpClient
import java.lang.reflect.Type
import java.util.*
import java.util.concurrent.TimeUnit
import kotlin.coroutines.resume
import kotlin.coroutines.resumeWithException

/**
 * 作者　　: 李坤
 * 创建时间: 2018/5/10 0010　下午 3:13
 * 邮箱　　：496546144@qq.com
 *
 *
 * 功能介绍：
 */
class MyApp : Application() {
    companion object {
        lateinit var app: MyApp
    }

    override fun onCreate() {
        app = this
        super.onCreate()
        LiteOrmUtil.init(this)
        LiteOrmUtil.setVersionCode(11)
        val params: MutableMap<String, Any> = HashMap()
        params["appver"] = "2.6.0"
        params["devicetype"] = "android"
        params["brand"] = "Xiaomi"
        params["deviceMode"] = "MI 5"
        params["sessionid"] = "5BY2XKL8CLDUSXXS7IF2VX55D8O5HHED"
        params["pid"] = "427"
        params["uid"] = "120842840"
        params["Apptag"] = "j725152534447786860o1AKfZFfCT4NWa39"
        params["timestamp"] = System.currentTimeMillis()
        val head: MutableMap<String, String> = HashMap()
        head["appver"] = "2.6.0"
        head["devicetype"] = "android"
        val mOkHttpClient: OkHttpClient = OkHttpClient.Builder()
            .readTimeout(100000, TimeUnit.MILLISECONDS)
            .writeTimeout(100000, TimeUnit.MILLISECONDS)
            .connectTimeout(100000, TimeUnit.MILLISECONDS)
            .cookieJar(CookieJarImpl(DBCookieStore()))
            .addInterceptor(MarvelSigningInterceptor())
            .addInterceptor(LoggingInterceptor())
            .build()
        OkHttpManage.init(this, mOkHttpClient)
        OkHttpManage.get().commonHeaders = head
        OkHttpManage.get().commonParams = params
        DownloadManager.initPath(this.cacheDir.path)
        Retrofit.get().init(
            createRequest = { MyHttpRequest(it.url) },
            execute = { request, result, params -> request.syncExecuteSus(result.resultType) }
        )
    }
}

/**
 * 协程 同步请求,Retorfit 是使用这种的
 * 使用回调方式转成协成的同步请求
 * @param resultType 数据类型
 */
suspend fun <T> HttpRequest.syncExecuteSus(resultType: Type): T {
    return suspendCancellableCoroutine { continuation ->
        try {
            var call: ExecuteCall? = null
            continuation.invokeOnCancellation {
                //当协程被取消的时候，取消网络请求
                call?.cancel()
            }
            val callback = object : AbsCallback<T>() {

                override fun onSuccess(responseBody: T) {
                    super.onSuccess(responseBody)
                    continuation.resume(responseBody)
                }

                override fun onError(e: HttpException) {
                    super.onError(e)
                    continuation.resumeWithException(e)
                }
            }
            callback.resultType = resultType
            if (!continuation.isCancelled) {
                call = execute(callback)
            }
        } catch (e: Exception) {
            continuation.resumeWithException(e)
        }
    }
}

suspend fun <T> testSuspend(): T {
    return withContext(Dispatchers.IO) {
        suspendCancellableCoroutine { continuation ->
            continuation.resumeWithException(throw RuntimeException("ddddddd"))
        }
    }
}

class MyHttpRequest(url: String) : HttpRequest(url) {
    override fun onBuildRequestBodyHasCommonParams() {
        super.onBuildRequestBodyHasCommonParams()
        toJson()
    }

}